#include <iostream>
using namespace std;

class Father{
    protected:
        string name;
        int *age;
    public:
        Father():age(new int (0)){}
        Father(string n, int a):name(n), age(new int(a)){}
        virtual ~Father(){
            cout << "Father 析构函数" << endl;
            delete age;
            age = nullptr;
        }
        virtual void show(void){
            cout << "Father show : " << name << " " << *age << endl;
        }
};

class Son:public Father{
    private:
        int *money;
    public:
        Son(){}
        Son(string n, int a, int m):Father(n, a), money(new int(m)){}
        ~Son(){
            cout << "Son 析构函数" << endl;
            delete money;
            money = nullptr;
        }
        void show(void){
            cout << "Son show : " << name << " " << *age << " " << *money << endl;
        }
};

int main(int argc, const char *argv[]){
    Son s1("小明", 18, 1000);
    s1.show();

    cout << "-------------------" << endl;
    Father *p1 = new Son("小红", 20, 2000);
    p1->show();
    // 如果父类的析构函数不是虚函数 此处的delete只会调用父类的析构函数
    // 不会调用子类的析构函数  会导致内存泄露！！
    delete p1;

    return 0;
}